/******************************************************************************
* File description: passwordui.cpp
* Function: 密码UI
* Current Version: 1.0.0
* Author: 张宇轩
* Date: 2024-08-19
*  -----
*  Copyright by 博坤生物 .
 *****************************************************************************/
#include "passwordui.h"

#include <QMessageBox>

/**
 * @brief 构造函数
 */
PasswordUI::PasswordUI()
{
    // 初始化
    this->setResizeMode(QQuickWidget::SizeRootObjectToView);
    this->setFixedSize(400, 303);
    this->setWindowFlags(Qt::Window | Qt::WindowCloseButtonHint);

    this->setWindowModality(Qt::ApplicationModal);
    this->setWindowTitle("密码");

    this->_rootContext = this->getContext();
    // 传入AnalysisConfigUI类
    this->rootContext()->setContextProperty("passwordUi", this);
    // 资源加载
    this->setSource(QUrl("qrc:/password.qml"));
    this->_rootObj = this->getRootObject();
    if(_rootObj)
    {
        // 确认
        connect(_rootObj, SIGNAL(submit(QString)), this, SLOT(onSubmit(QString)));
        // 取消
        connect(_rootObj, SIGNAL(cancel()), this, SLOT(onCancel()));
    }
}

/**
 * @brief 析构函数
 */
PasswordUI::~PasswordUI() {}

/**
 * @brief getContext 获取qmlcontext
 * @return qmlContext
 */
QQmlContext* PasswordUI::getContext() const
{
    return this->rootContext();
}

/**
 * @brief getRootObject 获取rootObject
 * @return rootObject
 */
QObject* PasswordUI::getRootObject() const
{
    return (QObject*)this->rootObject();
}

/**
 * @brief 确认槽函数
 * @param pwd 密码
 */
void PasswordUI::onSubmit(QString pwd)
{
    if(pwd == _initPwd)
    {
        // 发送密码正确信号
        emit PasswordUI::passwordIsCorrect();
        // 页面关闭
        this->close();
    }
    else
    {
        // 密码错误
        QMessageBox::critical(nullptr, "密码", "密码错误！");
    }
}

/**
 * @brief 取消槽函数
 */
void PasswordUI::onCancel()
{
    // 页面关闭
    this->close();
}

/**
 * @brief 关闭窗口事件
 * @param event 窗口事件
 */
void PasswordUI::closeEvent(QCloseEvent *event)
{
    emit PasswordUI::clearPassword();
}
